/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.DAO.SocketDAO;

import common.DAO.CategoryDAO;
import common.Objects.Category;
import common.xmlObjects.BaseConnectionXML;
import common.xmlObjects.CategoryXML;
import java.util.Vector;
import javaclientcp3110.xmlObjects.ClientXML;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Slasher
 */
class SocketCategoryDAO implements CategoryDAO {

    public Vector<Category> getCategorys() {
        Vector<Category> categorys = new Vector<Category>();
        ClientXML client = new ClientXML(ClientXML.GETALL, ClientXML.CATEGORY);

        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.SERVER);
        if (nodeList.getLength() > 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return null;
            }
            else {
                nodeList = node.getChildNodes();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    node = nodeList.item(i);
                    categorys.add(new CategoryXML(node).getObject());
                }
            }
        } else {
            return null;
        }
        return categorys;
    }

    public boolean createCategory(Category category) {
        ClientXML client = new ClientXML(ClientXML.ADD, ClientXML.COMPONENT);
        client.addChildNode(new CategoryXML(category));

        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.CLIENT);
        if (nodeList.getLength() > 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return false;
            }
            return true;
        }
        return false;
    }

    public boolean deleteCategory(int id) {
        ClientXML client = new ClientXML(ClientXML.DELETE, ClientXML.COMPONENT);
        client.addChildNode(new CategoryXML(String.valueOf(id),null));
        String responseXML = new SocketDAOFactory().sendXML(client.toXMLString());
        Document document = BaseConnectionXML.XMLStrTODOMDoc(responseXML);
        NodeList nodeList = document.getElementsByTagName(BaseConnectionXML.CLIENT);
        if (nodeList.getLength() > 0) {
            Node node = nodeList.item(0);
            Element element = (Element) node;
            String object = element.getAttribute(BaseConnectionXML.OBJECTNAME);

            if (object.equals(BaseConnectionXML.FAILED)) {
                return false;
            }
            return true;
        }
        return false;
    }

}
